using System;
using System.Security.Principal;

namespace PS.WebParts.Rollup.Query
{
    public class IdentityImpersonator
    {
        WindowsImpersonationContext ctx = null;
        public void UseAppPoolIdentity()
        {
            try
            {
                if (!WindowsIdentity.GetCurrent().IsSystem)
                {
                    ctx = WindowsIdentity.Impersonate(IntPtr.Zero);
                }
            }
            catch { }
        }

        public void ReturnToCurrentUser()
        {
            try
            {
                if (ctx != null)
                {
                    ctx.Undo();
                }
            }
            catch { }
        }
    }
}
